package org.zappee.peterk.custom.controller
{
	import org.zappee.peterk.custom.commands.SaveBugCommand;
	import org.zappee.peterk.custom.controller.interfaces.IBugFormController;
	import org.zappee.peterk.custom.model.interfaces.IBugModel;
	import org.zappee.peterk.custom.vo.Bug;
	import org.zappee.peterk.custom.vo.interfaces.IBug;

	public class BugFormController implements IBugFormController
	{
		protected var title: String;
		protected var description: String;
		protected var priority: String;
		protected var assignedTo: String;
		
		private var _bugModel: IBugModel;
		
		public function get bugModel(): IBugModel
		{
			return this._bugModel;
		}
		
		public function set bugModel( value: IBugModel ): void
		{
			this._bugModel = value;
		}
		
		public function BugFormController()
		{
		}

		public function updateTitle(value:String):void
		{
			this.title = value;
		}
		
		public function updateDescription(value:String):void
		{
			this.description = value;
		}
		
		public function updatePriority(value:String):void
		{
			this.priority = value;
		}
		
		public function updateAssignedTo(value:String):void
		{
			this.assignedTo = value;
		}
		
		/**
		 * Saves the bug to the data
		 * store.
		 */
		public function save(): void
		{
			var bug: IBug;
			
			if ( !validate() ) // failed
			{
				// TODO: Give user feedback
			}

			bug = new Bug();
			bug.active = true;
			bug.assignedTo = this.assignedTo;
			bug.description = this.description;
			bug.priority = this.priority;
			bug.title = this.title;
			bug.createdBy = "peter@moov2.com";
				
			var command: SaveBugCommand = new SaveBugCommand( bug, this._bugModel );
			command.execute();
		}
		
		/**
		 * Checks to see if the form is 
		 * valid.
		 */
		public function validate():Boolean
		{
			if ( 	this.title == null ||
					this.title == "")
				return false;
			
			if ( 	this.description == null ||
					this.description == "")	
				return false;
			
			if (	this.assignedTo == null ||
					this.assignedTo == "")
				return false;
			
			if (	this.priority == null ||
					this.priority == "")
				return false;
			
			return true;
		}
		
	}
}